<?php

namespace App\Models\Backend\System;

use App\Models\Model;
use Illuminate\Support\Facades\Log;

class Group extends Model
{
    const GROUP_FLAG_NORMAL = 0;
    const GROUP_FLAG_SCHOOL = 1;
    const GROUP_FLAG_GRADE  = 2;
    const GROUP_FLAG_CLASS  = 3;

    private static $group_flags = [
        self::GROUP_FLAG_NORMAL => '普通组',
        self::GROUP_FLAG_SCHOOL => '学校',
        self::GROUP_FLAG_GRADE  => '年级',
        self::GROUP_FLAG_CLASS  => '班级',
    ];

    public static function getGroupFlags()
    {
        return self::$group_flags;
    }


    /**
     * 保存来自中心 UAC 的组数据
     *
     * @param array $data 来自中心 UAC 的组数据数组
     */
    public static function saveGroupFromCenter($data)
    {

        $fields = ['group_name', 'thread_id', 'parent_id', 'group_flag', 'org_code', 'order_id'];
        $update_data = [];
        foreach ($fields as $field) {
            if (isset($data[$field])) {
                $update_data[$field] = $data[$field];
            }
        }
        $data['group_id'] = isset($data['group_id']) ? $data['group_id'] : 0;
        self::updateOrCreate(
            ['id' => $data['group_id']],
            $update_data
        );
    }

    /**
     * 保存来自中心 UAC 的组数据(rpc)
     *
     * @param array $data 来自中心 UAC 的组数据数组
     */
    public static function saveGroupFromCenter_rpc($data)
    {
        log::info('saveGroupFromCenter_rpc数据'.json_encode($data));
        $fields = ['group_id','group_name', 'thread_id', 'parent_id', 'group_flag', 'dec_grade_id', 'dec_classroom_id'];
        $update_data = [];
        foreach ($fields as $field) {
            if (isset($data[$field])) {
                $update_data[$field] = $data[$field];
            }
        }
        $data['group_id'] = isset($data['group_id']) ? $data['group_id'] : 0;
        self::updateOrCreate(
            ['id' => $data['group_id']],
            $update_data
        );
    }

    public function getContainGroups(){
        $groups = [$this->id];
        $lowerGroups = $this->where('parent_id', $this->id)->get(['id']);
        foreach ($lowerGroups as $lowerGroup){
            $groups = array_merge($lowerGroup->getContainGroups(), $groups);
        }
        return $groups;
    }

    /**
     * 获取从最上层组到当前组的数组
     *
     * @param boolean $name_only 是否只返回组名称数组
     * @return array 返回组对象数组，或组名称数组（当 $name_only = true 时）
     */
    public function getPath($name_only = true)
    {
        $groups = [$this];
        if ($this->parent_id > 0) {
            if ($name_only) {
                $groups = array_merge(self::find($this->parent_id)->getPath(), [$this->group_name]);
            }else{
                $groups = array_merge(self::find($this->parent_id)->getPath(), $groups);
            }
        }
        if ($name_only) {
            if ($this->parent_id > 0) {
                $names = [];
                foreach ($groups as $groupname) {
                    $names[] = $groupname;
                }
                return $names;
            }else{
                return [$this->group_name];
            }
        } else {
            return $groups;
        }
    }
}
